package goods

import (
	"encoding/json"
	// "fmt"
	"github.com/go-martini/martini"
	"github.com/martini-contrib/render"
	// "io/ioutil"
	"net/http"
	// "server/conf"
	"server/framework/API/R"
	"server/lib/Imartini"
	"server/lib/session"
	// "server/lib/upload"
	"server/web/model/shop/admin/goods"
	// "strconv"
	// "time"
)

func format(r render.Render, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) {
	page := func() string {
		if req.FormValue("page") == "" {
			return "1"
		} else {
			return req.FormValue("page")
		}
	}()
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	class := goods.Class(info["id"].(string))
	data, total := goods.Format(info["id"].(string), page)

	r.HTML("shop/admin/goods/format.html", map[string]interface{}{
		"class":  class,
		"format": data,
		"total":  total,
	})

}

func addformat(r render.Render, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	KX, _ := json.Marshal(req.Form["KX"])
	data := map[string]interface{}{
		"uid":     info["id"].(string),
		"name":    req.Form["name"][0],
		"classid": req.Form["classid"][0],
		"remarks": req.FormValue("remarks"),
		"KX":      string(KX),
	}

	goods.AddFormat(data)
	http.Redirect(res, req, req.URL.Path, http.StatusFound)
}

func delformat(params martini.Params, r render.Render, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) string {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	id := params["id"]

	return goods.DelFormat(id, info["id"].(string))
}

func updateformat(params martini.Params, res http.ResponseWriter, req *http.Request, mlog *Imartini.Mlog, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	p := make(map[string]interface{})
	p["name"] = req.FormValue("name")
	p["classid"] = req.FormValue("classid")
	p["remarks"] = req.FormValue("remarks")
	b, _ := json.Marshal(req.Form["KX"])
	p["KX"] = string(b)
	_, err := goods.UpdateFormat(info["id"].(string), params["id"], p)
	if err != nil {
		mlog.Printf("%v", err)
	}

	http.Redirect(res, req, "/admin/goods/format", http.StatusFound)
}

func getformatinfo(params martini.Params, r render.Render, res http.ResponseWriter, req *http.Request, g Imartini.G, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	class := goods.Formatinfo(info["id"].(string), params["id"])

	if len(class) > 0 {
		R.Write(res, g, map[string]interface{}{"format": class[1]})
	} else {
		R.Write(res, g, map[string]interface{}{})
	}
}

func getformat(params martini.Params, r render.Render, res http.ResponseWriter, req *http.Request, g Imartini.G, sess session.SESS) {
	// sess := session.GetAll(req)
	var info map[string]interface{}
	json.Unmarshal([]byte(sess["admin_info"]), &info)

	class := goods.GetFormat(info["id"].(string), params["classid"])
	data := make([]map[string]string, 0)
	for _, i := range class {
		data = append(data, i)
	}

	if len(class) > 0 {
		R.Write(res, g, map[string]interface{}{"format": data})
	} else {
		R.Write(res, g, map[string]interface{}{})
	}
}
